import pickle
from status import Status
class InterStatus:
    '''算法内部对象'''

    def __init__(self):
        self._count = 0
        self.turn = 0
        self.curr_status = Status.idle
        self.forward_count = 0
        self.forward_count_phase2 = 0
        self.padding_from = Status.idle
        self.address = '0.0.0.0'
        self.id = -1

    def to_str(self):
        ''' 转换成字符串 '''
        result = str(self._count) + str(self.turn) + str(self.curr_status) + str(self.forward_count) + str(
            self.forward_count_phase2) + str(self.padding_from)
        return result

    def change(self):
        result = str(self._count) + '|' + str(self.turn) + '|' + str(self.curr_status) + '|' + str(
            self.forward_count) + '|' + str(
            self.forward_count_phase2) + '|' + str(self.padding_from) + '|' + str(self.address) + '|' + str(
            self.id) + '|'
        return result

    def encodes(self) -> bytes:
        ''' 将InterStatus字节数组 '''
        return pickle.dumps(self)

    @staticmethod
    def try_from(raw_data):
        '''将字节数组转换为InterStatus实例对象'''
        return pickle.loads(raw_data)